/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    var ue = UE.getEditor('editor', {initialFrameHeight: 250});
    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-type"><option value="">退房类型</option><option value="1">到期退房</option><option value="2">提前退房</option><option value="3">强制退房</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/retire/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-type');

    $('#delete-retire-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.retire-id').val(id);
    });
    $('#delete-retire-dialog').on('hide.bs.modal', function(){
        $(this).find('.retire-id').val('');
    });
    $('#delete-retire-dialog .btn-save').on('click', function(){
        var that = $('#delete-retire-dialog');
        var id = that.find('.retire-id').val();
        sendAjax('/v1/manager/delete/retire', 'post', {id:id}, function(json){
            that.modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#view-retire-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var that = $(this);
        if (id) {
            sendAjax('/v1/manager/retire/detail.json', 'get', {id:id}, function(json){
                var retire = json.body.data;
                if (retire) {
                    that.find('.retire-id').val(id);
                    that.find('.apart-id').val(retire.apart_id);
                    that.find('.apart-name').text(retire.name);
                    var type = getRetireType(retire.type);
                    that.find('.retire-type').text(type);
                    var checkout_at = retire.checkout_at == '0000-00-00 00:00:00' ? '' : retire.checkout_at;
                    that.find('.checkout-at').val(checkout_at);
                    that.find('input[name="is-rent-pay"]').each(function(){
                        if ($(this).val() == retire.is_rent_pay) {
                            $(this).prop('checked', true);
                        }
                    });
                    that.find('input[name="is-water-fee-pay"]').each(function(){
                        if ($(this).val() == retire.is_water_fee_pay) {
                            $(this).prop('checked', true);
                        }
                    });
                    that.find('input[name="is-electric-fee-pay"]').each(function(){
                        if ($(this).val() == retire.is_electric_fee_pay) {
                            $(this).prop('checked', true);
                        }
                    });
                    that.find('input[name="is-gas-fee-pay"]').each(function(){
                        if ($(this).val() == retire.is_gas_fee_pay) {
                            $(this).prop('checked', true);
                        }
                    });
                    that.find('input[name="is-device-safe"]').each(function(){
                        if ($(this).val() == retire.is_device_safe) {
                            $(this).prop('checked', true);
                        }
                    });
                    that.find('.retire-remark').val(retire.remark);
                    ue.ready(function(){
                        this.setContent(retire.remark);
                    });
                }
            });
        }
    });

    $('#view-retire-dialog').on('hide.bs.modal', function(){

        if (!$(this).hasClass('in')) {
            $(this).find('.retire-id').val('');
            $(this).find('.apart-id').val('');
            $(this).find('.apart-name').text('');
            $(this).find('.retire-type').text('');
            $(this).find('.checkout-at').val('');
            $(this).find('input[name="is-rent-pay"]:last').prop('checked', true);
            $(this).find('input[name="is-water-fee-pay"]:last').prop('checked', true);
            $(this).find('input[name="is-electric-fee-pay"]:last').prop('checked', true);
            $(this).find('input[name="is-gas-fee-pay"]:last').prop('checked', true);
            $(this).find('input[name="is-device-safe"]:last').prop('checked', true);
            $(this).find('.retire-remark').val('');
        }
    });

    $('#view-retire-dialog .btn-save').on('click', function(){
        var that = $('#view-retire-dialog');
        $('[name="remark"]').val(ue.getContent());
        var data = {
            id: that.find('.retire-id').val(),
            checkout_at: that.find('.checkout-at').val(),
            is_rent_pay: that.find('input[name="is-rent-pay"]:checked').val(),
            is_water_fee_pay: that.find('input[name="is-water-fee-pay"]:checked').val(),
            is_electric_fee_pay: that.find('input[name="is-electric-fee-pay"]:checked').val(),
            is_gas_fee_pay: that.find('input[name="is-gas-fee-pay"]:checked').val(),
            is_device_safe: that.find('input[name="is-device-safe"]:checked').val(),
            remark: that.find('.retire-remark').val()
        };
        sendAjax('/v1/manager/save/retire', 'post', {data:data}, function(json){
            that.modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, toolbar) {
    $("#retire-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.type = $('.select-type').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'rent_user_name',
            title: '承租人姓名（工号）'
        }, {
            field: 'checkout_at',
            title: '退房时间'
        }, {
            field: 'type',
            title: '类型',
            formatter: function (value) {
                return getRetireType(value);
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-retire" data-toggle="modal" data-target="#view-retire-dialog" data-id="'+value+'">详情</a>';
                if (row.user_id != row.created_by) action += '<a class="btn btn-xs btn-danger delete-retire" data-toggle="modal" data-target="#delete-retire-dialog" data-id="'+value+'">删除</a>';
                return action;
            }
        }, {
            field: 'user_id',
            title: '承租人',
            visible: false
        }, {
            field: 'created_by',
            title: '发起人',
            visible: false
        }]
    });
}

function refreshList() {
    $('#retire-table').bootstrapTable('refresh');
}

function getRetireType(type) {
    switch (type) {
        case 1 : return '到期退房';
        case 2 : return '提前退房';
        case 3 : return '强制退房';
        default: return '到期退房';
    }
}